package com.example.shop_2208.comment

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.shop_2208.entity.CommentEnityResult
import com.example.shop_2208.entity.ParamComment
import com.example.shop_2208.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class CommentViewModel:BaseViewModel() {

    val getSuccess = MutableLiveData<CommentEnityResult>()
    val getFail = MutableLiveData<String>()

    fun getComment(){
        viewModelScope.launch {
            try {
                getSuccess.value = apiServer.getComment()
            }catch (e:Exception){
                getFail.value = e.message
            }
        }
    }

    val getChildSuccess = MutableLiveData<CommentEnityResult>()
    val getChildFail = MutableLiveData<String>()
    fun getChildComment(rootId:Int){
        viewModelScope.launch {
            try {
                getChildSuccess.value = apiServer.getChildComment(rootId)
            }catch (e:Exception){
                getChildFail.value = e.message
            }
        }
    }

    val commentSuccess = MutableLiveData<CommentEnityResult>()
    val commentFail = MutableLiveData<String>()
    fun comment(msg:String){
        viewModelScope.launch {
            try {
                val param = ParamComment(msg,103)
                commentSuccess.value = apiServer.comment(param)
            }catch (e:Exception){
                commentFail.value = e.message
            }
        }
    }
}